<template>
    <component is="a-modal" v-model:visible="visible" :width="800" :title="title" :mask-closable="false"
      :ok-loading="loading" @cancel="close" @before-ok="submit">
      <div>
        <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
            <a-form-item label="快递公司" field="company_id">
              <sa-select style="width: 60%;" v-model="formData.company_id" :data="paramsData.company"
                :fieldNames="{ value: 'id', label: 'name' }"></sa-select>
            </a-form-item>
            <a-form-item label="快递单号" field="code">
              <a-input style="width: 60%;" placeholder="请输入" :allow-clear="true" v-model="formData.code"></a-input>
            </a-form-item>
            <a-form-item label="快递发货时间" field="shipping_time">
              <a-date-picker show-time style="width: 60%;" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss" placeholder="请选择"
              v-model="formData.shipping_time" />
            </a-form-item>
            <a-form-item label="备注" field="note">
              <a-input style="width: 60%;" placeholder="请输入" :allow-clear="true" v-model="formData.note"></a-input>
            </a-form-item>
  
        </a-form>
      </div>
    </component>
  </template>
  <script setup>
  import { onMounted, ref, reactive, computed, watch } from 'vue'
  import AsApi from '@/api/system/afterSale'
  import SaSelect from '@cps/sa-select/index.vue'
  import { Message } from '@arco-design/web-vue'
  import { throttle } from 'lodash';
  import tool from '@/utils/tool'
  const emit = defineEmits(["success"]);
  const visible = ref(false);
  const formRef = ref(null);
  const title = ref('')
  const mode = ref('')
  const loading = ref(false);
  const formData = reactive({
    id: '',
    code: '',
    order_id: '',
    shipping_time: '',
    company_id: '',
    note: '',
  })
  const rules = ref({
    company_id: [{ required: true, message: "请选择" }],
    shipping_time: [{ required: true, message: "请选择" }],
    code: [{ required: true, message: "请输入" }],
  
  });
  const paramsData = ref({})
  const getParams = async () => {
    const { data } = await AsApi.aftersale_output_config(formData.id)
    paramsData.value = { ...data }
    // formData.company_id = data.info?.express_name
    // formData.code = data.info?.express_code
    // formData.shipping_time = data.info?.send_time
    // formData.note=data.info?.express_note
   
  }
  const submit = throttle(async (done) => {
    const validate = await formRef.value?.validate();
    if (!validate) {
      loading.value = true;
      try {
        let result = {};
        let data = {
            id: formData.id,
            company_id: formData.company_id,
            code: formData.code,
            shipping_time: formData.shipping_time,
            note: formData.note,
          }
          
          result = await AsApi.edit_delivery(data);
        if (result.code === 200) {
          Message.success("操作成功");
          emit("success");
          done(true);
        }
        // 防止连续点击提交
        setTimeout(() => {
          loading.value = false;
        }, 500);
      } catch {
        loading.value = false;
      }
    }
    done(false);
  }, 100);
  const open = async (type, data) => {
    console.log('1111', data);
    formData.id = data.id;
    formRef.value.resetFields();
    mode.value = type
    visible.value = true;
    title.value = '修改发货'
    await getParams()
  }
  defineExpose({ open });
  </script>
  
  <!-- 000999888777 -->